package com.molichuxing.gateway.bff.goods.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.goods.convertor.Convert;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsExclusiveCreateVo;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsSubscribeCreateVo;
import com.molichuxing.gateway.bff.goods.entity.response.*;
import com.molichuxing.gateway.bff.goods.services.GoodsBffService;
import com.molichuxing.services.business.dto.request.create.GoodsSubscribeCreateDto;
import com.molichuxing.services.business.dto.response.GoodsBaseBizDto;
import com.molichuxing.services.business.dto.response.GoodsBizBizDto;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.response.ContractTemplateDto;
import com.molichuxing.services.infrastructure.dto.response.VehiclePictureDto;
import com.molichuxing.services.infrastructure.service.ContractTemplateService;
import com.molichuxing.services.infrastructure.service.GoodsSpecConfigService;
import com.molichuxing.services.infrastructure.service.VehiclePictureService;
import com.molichuxing.services.infrastructure.service.VehicleService;
import com.molichuxing.services.property.GoodsForbidSaleEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品信息相关bff
 * 
 * @author yixuelin
 * @date 2019年8月13日
 *
 */
@Service("goodsBffService")
public class GoodsBffServiceImpl implements GoodsBffService {
	private static final Logger logger = LoggerFactory.getLogger(GoodsBffServiceImpl.class);

	@Resource
	private GoodsBizService goodsBizService;

	@Resource
	private VehicleBizService vehicleBizService;

	@Resource
	private VehiclePictureService vehiclePictureService;

	@Resource
	private GoodsSpecConfigService goodsSpecConfigService;

	@Resource
    private ContractTemplateService contractTemplateService;

	@Override
	public Paged<GoodsVo> getGoodsList(Map<String, Object> selectParams) {
		if(null == selectParams.get("goodsType")) {
			return null;
		}
		Paged<GoodsBizBizDto> page = goodsBizService.getGoodsList(GoodsTypeEnum.getName(ObjectCastUtil.castInteger(selectParams.get("goodsType"))), ObjectCastUtil.castString(selectParams.get("code")),
				ObjectCastUtil.castInteger(selectParams.get("carStyleId")), null, ObjectCastUtil.castInteger(selectParams.get("pageNum")), ObjectCastUtil.castInteger(selectParams.get("pageSize")));
		List<GoodsBizBizDto> goodsList = page.getList();
		Map<Integer, String> vehicleMap = new HashMap<>();
		Map<Integer, String> vehicleImgMap = new HashMap<>();
		if(null != goodsList && goodsList.size() > 0) {
			for(GoodsBizBizDto goodsBizBizDto : goodsList) {
				String vehicleName = vehicleBizService.getVehicleName(goodsBizBizDto.getVehicleStyleId());
				vehicleMap.put(goodsBizBizDto.getGoodsId(), vehicleName);
				List<VehiclePictureDto> vehiclePictureList = vehiclePictureService.getVehiclePictureByVehicleId(goodsBizBizDto.getVehicleStyleId());
				if((null != vehiclePictureList)&&(vehiclePictureList.size() > 0)) {
					for(VehiclePictureDto vehiclePictureDto : vehiclePictureList) {
						if(vehiclePictureDto.getIsWhiteBg() && 1 == vehiclePictureDto.getPicDetailType()) {
							vehicleImgMap.put(goodsBizBizDto.getGoodsId(), vehiclePictureDto.getServerUrl());
							break;
						}
					}
				}

			}
		}
		return Convert.toGoodsPage(page, vehicleMap, vehicleImgMap);
	}

	@Override
	public GoodsBaseVo getBaseGoods(Integer goodsId)throws Exception {
		GoodsBaseBizDto goodsBaseBizDto = goodsBizService.getBaseGoods(goodsId);
		String goodsPicture = null;
		if(null != goodsBaseBizDto) {
			List<VehiclePictureDto> vehiclePictureList = vehiclePictureService.getVehiclePictureByVehicleId(goodsBaseBizDto.getVehicleStyleId());
			if((null != vehiclePictureList)&&(vehiclePictureList.size() > 0)) {
				for(VehiclePictureDto vehiclePictureDto : vehiclePictureList) {
					if(vehiclePictureDto.getIsWhiteBg() && 1 == vehiclePictureDto.getPicDetailType()) {
						goodsPicture = vehiclePictureDto.getServerUrl();
						break;
					}
				}
			}
		}
		return Convert.toGoodsBaseVo(goodsBaseBizDto, goodsPicture);
	}

	@Override
	public GoodsDetailVo getDetailGoods(Integer goodsId) throws Exception {
		return Convert.toGoodsDetailVo(goodsBizService.getDetailGoods(goodsId));
	}

	@Override
	public Boolean createExclusiveGoods(List<GoodsExclusiveCreateVo> createDtoList) throws Exception {
		for(GoodsExclusiveCreateVo createDto : createDtoList) {
			goodsBizService.createExclusiveGoods(Convert.toGoodsExclusiveCreate(createDto));
		}
		return true;
	}

	@Override
	public Boolean createSubscribeGoods(List<GoodsSubscribeCreateVo> createDtoList) throws Exception {
		for(GoodsSubscribeCreateVo createDto : createDtoList) {
			goodsBizService.createSubscribeGoods(Convert.toGoodsSubscribeCreate(createDto));
		}
		return true;
	}

	@Override
	public Boolean settingGoodsSale(int goodsId, Byte isForbidSale) throws Exception {
		return goodsBizService.settingGoodsSale(goodsId, GoodsForbidSaleEnum.getName(isForbidSale));
	}

	@Override
	public List<GoodsSpecConfigVo> getGoodsProductSpecList(List<String> specList, int goodsType) throws Exception {
		return Convert.toGoodsSpecConfigVo(goodsSpecConfigService.getGoodsProductSpecList(specList, GoodsTypeEnum.getName(goodsType)));
	}

    @Override
    public Paged<ContractTemplateVo> getContractTemplateList(Integer businessType) {
		Paged<ContractTemplateVo> paged = new Paged<>();
		List<ContractTemplateVo> result = Convert.toContractTemplateVo(contractTemplateService.getByGoodsType(businessType));
		paged.setList(result);
		List<TabDto> tabList = new ArrayList<>();
		for(GoodsTypeEnum statusEnum : GoodsTypeEnum.values()) {
			List<ContractTemplateDto> resultData = contractTemplateService.getByGoodsType(statusEnum.getValue());
			TabDto tabDto = new TabDto();
			tabDto.setCount(resultData.size());
			tabDto.setName(statusEnum.getName());
			tabDto.setType(statusEnum.getValue());
			tabList.add(tabDto);
		}
		paged.setTotal(result.size());
		paged.setTab(tabList);
		return paged;
    }
}
